package com.visualexpress.wrapper.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.visualexpress.dao.LocationDAO;
import com.visualexpress.dao.LocationPairShipmentDAO;
import com.visualexpress.dao.ShipmentDAO;
import com.visualexpress.dao.TimeLocationDAO;
import com.visualexpress.datacreation.CreateDatabaseData;
import com.visualexpress.domain.Location;
import com.visualexpress.domain.LocationPairShipment;
import com.visualexpress.domain.Shipment;
import com.visualexpress.domain.TimeLocation;
import com.visualexpress.wrapper.EMS;
import com.visualexpress.wrapper.Info;
import com.visualexpress.wrapper.word.LocationAnalysis;

public class EMS2Shipment {
	private ShipmentDAO shipDao;
	private LocationDAO locationDao;
	private TimeLocationDAO tlDao;
	private LocationAnalysis la;
	private LocationPairShipmentDAO lpsDao;
	public EMS2Shipment() {
		shipDao = new ShipmentDAO();
		locationDao = new LocationDAO();
		tlDao = new TimeLocationDAO();
		la = new LocationAnalysis();
		lpsDao = new LocationPairShipmentDAO();
	}
	public Shipment changeEMS2Shipment(EMS ems)
	{
		Shipment s = new Shipment();
		s.setShipmentNum(ems.getMailNum());
		s.setState(ems.isReceived());
		shipDao.save(s);
        ArrayList<Info> infos = ems.getInfos();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-ddHH-mm-ss");
        for(int i=0; i<infos.size() ; i++)
        {
        	try {
        		Info info = infos.get(i);
				Date date = sdf.parse(info.getTime());
	        	Location l = this.parseLocation(info.getAddress());
	        	TimeLocation tl = new TimeLocation();
	        	LocationPairShipment lps = new LocationPairShipment();
	        	tl.setShipment(s);
	        	tl.setLocation(l);
	        	tl.setLocationNumber(i+1);
	        	tl.setArrivalTime(date);
	        	lps.setShipment(s);
	        	lps.setLoc1(l);
	        	if(i==0)
	        	{
	    			s.setStartLocation(l);
	        	}
	    		if(i==(infos.size()-1))
	    		{
	    			s.setEndLocation(l);
	    			tl.setLeaveTime(date);
	    			lps.setLoc2(l);
	        	}else
	        	{
	        		tl.setLeaveTime(sdf.parse(infos.get(i+1).getTime()));
	        		Location l2 = this.parseLocation(infos.get(i+1).getAddress());
	        		lps.setLoc2(l2);
	        	}
	    		tlDao.save(tl);
	    		lpsDao.save(lps);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
          }
        shipDao.attachDirty(s);
		return s;
	}
	private Location parseLocation(String address)
	{
		String location = la.getRealLocation(address);
		List<Location> ls = locationDao.findByName(location);
    	Location l;
    	if(ls.size()!=0)
    	{
    		l = ls.get(0);
    	}else
    	{
    		l = new Location();
    		l.setName(location);
    		CreateDatabaseData.GetInfoFromURL(l);
    		locationDao.save(l);
    	}
    	return l;
	}
}
